#include "StdAfx.h"
#include "ListMgr.h"
#include "BBViewer.h"

CListMgr::CListMgr(void)
{
	LoadText();
}

CListMgr::~CListMgr(void)
{
}

//////////////////////////////////////////////////////////////////////////
void CListMgr::InsertRecList(void *pItemTime)
{
	CTimeList *pItem = (CTimeList *)pItemTime;
	CTimeList *ptIndex;

	ptIndex = new CTimeList();
	*ptIndex = *pItem;
	m_arrRecList[ptIndex->GetRecType()].Add(ptIndex);

}

void CListMgr::ResetRecList()
{
	int i, dwRecType;
	CTimeList *ptIndex;
	for (dwRecType=0;dwRecType<MAX_REC_TYPE;dwRecType++)
	{
		for (i=0;i<m_arrRecList[dwRecType].GetSize();i++)
		{
			ptIndex = m_arrRecList[dwRecType].GetAt(i);
			delete ptIndex;
		}
		m_arrRecList[dwRecType].RemoveAll();
	}
}

void CListMgr::SortRecList()
{
	HANDLE hThread;
	UINT uID;
	hThread = (HANDLE)_beginthreadex(NULL, 0, _procSort, this, 0, &uID);
	WaitForSingleObject(hThread, INFINITE);
	CloseHandle(hThread);
}

UINT __stdcall CListMgr::_procSort(LPVOID lpParam)
{
	CListMgr *pListMgr = (CListMgr *)lpParam;
	pListMgr->procSort();
	_endthreadex(0);
	return 0;
}

void CListMgr::procSort()
{
	MergeSort();
}

//////////////////////////////////////////////////////////////////////////
void CListMgr::MergeSort()
{
	int nArr;
	int i, j;
	int nCompare;
	CTimeList *ptElement1, *ptElement2;

	CSortedArray <CTimeList *, CTimeList *> *pArrIndex;
	CSortedArray <CTimeList *, CTimeList *> arrWorkIndex;

	int nWidth;
	int n[2], nInc;
	int nLeft, nRight, nEnd;
	int nLength;

	int nLoop;
	nLoop = MAX_REC_TYPE;

	for (nArr=0;nArr<nLoop;nArr++)
	{
		pArrIndex = &m_arrRecList[nArr];

		nLength = pArrIndex->GetSize();
		arrWorkIndex.RemoveAll();
		arrWorkIndex.SetSize(nLength);

		for (nWidth=1;nWidth<nLength;nWidth = 2*nWidth)
		{
			for (i=0;i<nLength;i=i+2*nWidth)
			{
				nLeft = i;
				nRight = (i+nWidth) < nLength ? i+nWidth : nLength;
				nEnd = (i+2*nWidth) < nLength ? i+2*nWidth : nLength;

				n[0] = nLeft;
				n[1] = nRight;

				for (j=nLeft;j<nEnd;j++)
				{
					if (n[0]<nRight)
					{
						if (n[1]>=nEnd)
							nInc = 0;
						else
						{
							ptElement1 = pArrIndex->GetAt(n[0]);
							ptElement2 = pArrIndex->GetAt(n[1]);

							nCompare = ptElement1->CompareTime(*ptElement2);
							switch (nCompare)
							{
							case 1:
							case 3:
							case 4:
							case 5:
							case 6:
								nInc = 1;
								break;
							default:
								nInc = 0;
								break;
							}

						}
					}
					else
					{
						nInc = 1;
					}

					arrWorkIndex.InsertAt(j, pArrIndex->GetAt(n[nInc]));
					arrWorkIndex.RemoveAt(j+1);
					n[nInc] += 1;
				}

			}

			pArrIndex->RemoveAll();
			for (i=0;i<arrWorkIndex.GetSize();i++)
				pArrIndex->Add(arrWorkIndex.GetAt(i));
		}

	}

	for (nArr=0;nArr<nLoop;nArr++)
	{
		pArrIndex = &m_arrRecList[nArr];

		for (i=0;i<pArrIndex->GetSize()-1;i++)
		{
			ptElement1 = pArrIndex->GetAt(i);
			ptElement2 = pArrIndex->GetAt(i+1);

			//////////////////////////////////////////////////////////////////////////
			//2013.02.13
			if (!ptElement1->GetPlayAllStatus())
			{
				nCompare = ptElement1->CompareTime(*ptElement2);
				switch (nCompare)
				{
				case -2:
				case -3:
				case -6:
					ptElement1->SetEndTime(ptElement2->GetStartTime(), ptElement2->GetStartMilliSec());
					break;
				}
			}
			//////////////////////////////////////////////////////////////////////////
		}
	}
}
//////////////////////////////////////////////////////////////////////////
CString CListMgr::GetRecTypeString(DWORD dwRecType)
{
	if ((int)dwRecType<m_sarrText.GetCount())
		return m_sarrText.GetAt(dwRecType);
	return _T("");
}

CString CListMgr::GetString(int nIdx)
{
	return m_sarrText.GetAt(MAX_REC_TYPE+nIdx);
}

void CListMgr::RefreshLang()
{
	LoadText();
}

void CListMgr::LoadText()
{
	m_sarrText.RemoveAll();
	m_sarrText.Add(theApp.GetUIString(APPNAME_LIST, KEYNAME_LIST_NORMAL, _T("Normal")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_LIST, KEYNAME_LIST_GSENSOR, _T("G-Sensor")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_LIST, KEYNAME_LIST_PARKING, _T("Parking")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_LIST, KEYNAME_LIST_MANUAL, _T("Manual")));

	m_sarrText.Add(theApp.GetUIString(APPNAME_LIST, KEYNAME_LIST_DATE, _T("Date")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_LIST, KEYNAME_LIST_HOUR, _T("Hour")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_LIST, KEYNAME_LIST_START_TIME, _T("Start Time")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_LIST, KEYNAME_LIST_END_TIME, _T("End Time")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_LIST, KEYNAME_LIST_REC_TYPE, _T("Rec Type")));
	m_sarrText.Add(theApp.GetUIString(APPNAME_LIST, KEYNAME_LIST_EVENT, _T("Event")));
}